<?php
declare (strict_types=1);

namespace app\common\model;

use think\facade\Event;

class Carousel extends Base
{
    protected $name = 'carousel';

    public function getTypeAttr($value)
    {
        return carouselType($value);
    }

    public static function _all($map, $param, $hasOne, $like = [], $order = 'create_time,desc', $append = [], $filed = '*', $export = false): array
    {
        try {
            $page = $param['page'] ?? 1;
            $limit = $param['limit'] ?? 10;

            !$order && $order = 'create_time,desc';

            if(!isset($map['is_del'])){
                $map['is_del'] = 0;
            }
            $type = null;
            if(isset($map['type'])){
                $type = $map['type'];
                unset($map['type']);
            }
            $that = parent::where($map);

            if($type == 1) $that = $that->whereIn('type', [1,3]);
            else $that = $that->where('type',2);

            if ($hasOne) {
                $that = $that->with($hasOne);
            }

            if (isset($param['times']) && $param['times']) {
                $times = explode(' - ', $param['times']);
                $that->whereBetweenTime('create_time', strtotime($times[0]), strtotime($times[1]));
            }

            if (isset($param['otherTimes']) && $param['otherTimes']) {
                foreach ($param['otherTimes'] as $k => $v) {
                    if (empty($v)) continue;
                    $decode = explode(' - ', $v);
                    $that->whereBetweenTime($k, strtotime($decode[0]), strtotime($decode[1]));
                }
            }

            foreach ($like as $v) {
                foreach ($v as $key => $vo) {
                    $that->whereLike($key, '%' . trim($vo) . '%');
                }
            }
            $order = explode(',', $order);
            if (!$export) {

                $list = $that->page(intval($page), intval($limit))->field($filed)->order($order[0], $order[1])->select();

                if (!empty($append)) {
                    $list->append($append);
                }
                return ['code' => 0, 'data' => $list, 'count' => getCount($that->fetchSql()->select())];
            } else {

                $list = $that->field($filed)->order($order[0], $order[1])->select();
                if (!empty($append)) {
                    $list->append($append);
                }
                return ['code' => 0, 'data' => $list];
            }

        } catch (\Exception $e) {
            dump($e);
            die;
            return [];
        }
    }

    public static function _save($param): array
    {
        try {
            if(empty($param['link_type'])) $param['link_type'] = 1;
            if($param['link_type'] == 2){
                $arr = [];
                !empty($param['cate_1']) && $arr[] = $param['cate_1'];
                !empty($param['cate_2']) && $arr[] = $param['cate_2'];
                !empty($param['cate_3']) && $arr[] = $param['cate_3'];
                $param['link_id'] = implode(',', $arr);
            }elseif($param['link_type'] == 3){
                $param['link_id'] = $param['product_id'];
            }else{
                $param['link_id'] = 0;
            }

            if (isset($param['id']) && $param['id']) {
                $model = parent::update($param);
                $model->operation = 'save';
            } else {
                $model = parent::create($param);
                $model->operation = 'create';
            }

            Event::trigger('Logs', $model);

            return ['code' => 1, 'msg' => '操作成功'];
        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }
}
